package name.wilbur.course.db.bigone.ui;

import java.util.Map;

import name.wilbur.course.db.bigone.control.ArticleMgr;
import name.wilbur.course.db.bigone.model.Article;
import name.wilbur.course.db.bigone.model.BaseDBModel;
import name.wilbur.course.db.bigone.ui.custom.ListModel;
import name.wilbur.course.db.bigone.util.MysqlTimestampWrapper;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ArticleWindow extends BaseWindow {
	private boolean editable;
	
	//	private Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="10,10"
	private Label themeL = null;
	private Text themeT = null;
	private Label contentL = null;
	private Text contentTextArea = null;
	private ArticleMgr mgr;
	private ListModel<BaseDBModel> articleList;
	
	public ArticleWindow() {
		mgr = new ArticleMgr();
		editable = true;
	}
	
	public ArticleWindow(Display disp, ListModel<BaseDBModel> articleList, Map<String, Object> session, boolean editable) {
		super(disp);
		mgr = new ArticleMgr();
		mgr.setSession(session);
		this.articleList = articleList;
		this.editable = editable;
	}
	
	public ArticleWindow(Display disp, Article article, ListModel<BaseDBModel> articleList, Map<String, Object> session, boolean editable) {
		super(disp);
		mgr = new ArticleMgr(article);
		mgr.setSession(session);
		this.articleList = articleList;
		this.editable = editable;
	}
	
	/**
	 * This method initializes sShell
	 */
	public void createSShell() {
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 7;
		sShell = new Shell();
		sShell.setText("日志");
		sShell.setLayout(gridLayout);
		sShell.setSize(new Point(347, 225));
		themeL = new Label(sShell, SWT.NONE);
		themeL.setLayoutData(new GridData(GridData.BEGINNING, GridData.FILL, true, false, 1, 1));
		themeL.setText("主题");
		themeT = new Text(sShell, SWT.BORDER);
		themeT.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, 6, 1));
		themeT.setEditable(editable);
		contentL = new Label(sShell, SWT.NONE);
		contentL.setLayoutData(new GridData(GridData.BEGINNING, GridData.FILL, true, false, 1, 5));
		contentL.setText("内容");
		contentTextArea = new Text(sShell, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		contentTextArea.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 6, 5));
		contentTextArea.setEditable(editable);
		
		GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, false, 1, 1);
		Label filler = new Label(sShell, SWT.None);
		filler.setLayoutData(gridData);
		Button resetB = new Button(sShell, SWT.None);
		resetB.setText("重置");
		resetB.setEnabled(editable);
		resetB.setLayoutData(gridData);
		resetB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				themeT.setText("");
				contentTextArea.setText("");
			}
		});
		Button submitB = new Button(sShell, SWT.None);
		submitB.setText("确认");
		submitB.setLayoutData(gridData);
		submitB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				if (editable) {
					Article ar;
					boolean createP = false;
					if (mgr.getMyArticle() == null) {
						ar = new Article();
						createP = true;
						ar.setArticleCreateTime(MysqlTimestampWrapper.getCurrentTimestamp());
					} else {
						ar = mgr.getMyArticle();
					}
					ar.setArticleAuthorID(mgr.getMe().getPk()); 
					ar.setArticleTitle(themeT.getText());
					ar.setArticleContent(contentTextArea.getText());
					
					if (createP) {
						mgr.addNewArticle(ar);
						articleList.add(ar);
					} else {
						mgr.modifyArticle(ar);
					}
				}
				close();
			}
		});
	}

	public void initWindowData() {
		if (mgr.getMyArticle() != null) {
			 themeT.setText(mgr.getMyArticle().getArticleTitle());
			 contentTextArea.setText(mgr.getMyArticle().getArticleContent());
		}
	}
}
